import gui.ExitListener;


import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import control.Control;

/**
 * @param args
 */

public class Main {

	private String sourceFilePath;	
	private String destinationFilePath;
	private Control controller;

	public static void main(String[] args) {
		Main main = new Main();
		main.run();
	}
	
	public void run() {
		JFrame f = new JFrame("GPS Reference Converter");
	    f.setSize(400, 150);
	    Container content = f.getContentPane();
	    content.setBackground(Color.white);
	    content.setLayout(new FlowLayout());
	    
	    content.add(new JLabel("Choose a File to convert"));
	    JButton browse = new JButton("...browse");
	    
	    JTextField filePathLabel = new JTextField("blablabla");
	    filePathLabel.setSize(1000, 10000);

	    content.add(browse);
	    content.add(filePathLabel);
	    JButton save = new JButton("convert and save");
	    content.add(save);
	    f.addWindowListener(new ExitListener());
	    f.setVisible(true);


   

	    ActionListener chooseFile = new ActionListener() {
		   public void actionPerformed(ActionEvent actionEvent) {
		    	JFileChooser fileChooser = new JFileChooser();
		    	fileChooser.showOpenDialog(null);
		  		
		  		sourceFilePath = fileChooser.getSelectedFile().getAbsolutePath();
//		  		Main.this.sourceFilePath = "P:/downtoStuttgart.gpx";
//		  		sourceFile = fileChooser.getSelectedFile();
//		  		System.out.println(sourceFilePath);

		  		 parse();		  		
		  		//filePathLabel.setText(filePath);		  		 
		      }    
	    };
	    
	    ActionListener saveFile = new ActionListener() {
	      public void actionPerformed(ActionEvent actionEvent) {
	    	JFileChooser fileChooser1 = new JFileChooser();
	  		fileChooser1.showSaveDialog(null);
	  		
	  		destinationFilePath = fileChooser1.getSelectedFile().getAbsolutePath();
	  		save();
	      }
	    };	    
    
	    browse.addActionListener(chooseFile);	    
	    save.addActionListener(saveFile);
	}

	public void parse() {
		// TODO Auto-generated method stub
		controller = new Control();	
		controller.evaluateData(sourceFilePath);
	}
	
	public void save() {
		controller.writeKmlFile(destinationFilePath);
	}	
}
